import version from 'version'
import domain from 'ad-ui-domain'
import createReducer from 'ad-ui-reducer'
import cache, { BASICENUMS, BASICENUMS_VERSION } from 'cache'
import { AppState } from '@stores/interface'
import { isEmpty } from 'ad-ui-utils'

interface IBasics {
  id: string
  name: string
  code: string
  type: string
  nameCn?: string
  nameEn?: string
  [propName: string]: any
}

const Model = 'Map'

const Config = {
  findBasicEnum: (enitity: AppState, response: IBasics[]) => {
    if (!isEmpty(response)) {
      cache.set(BASICENUMS, response)
      cache.set(BASICENUMS_VERSION, version[BASICENUMS_VERSION])
    }
    return domain.create(Model, { data: response, _loading_: false })
  },
}

export default createReducer(Model, Config)
